
             
/**
 * 
 *
 * Copyright (c) 2012-2013 JLLT Corp. 2012-2013,All Rights Reserved.
 * This software is published under the 金麟龙腾商贸有限公司.
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file. 
 *
 * @File name:  TProductDao.java 
 * @Create on:  2012-06-28 21:05:412
 * @Author      :  高宇舟
 *
 * @ChangeList
 * ---------------------------------------------------
 * Date         Editor              ChangeReasons
 *
 */
           
        package com.boho.ecp.product.dao;

            
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.LinkedList;
import java.util.List;

import cn.boho.framework.po.CQExp;
import cn.boho.framework.po.CQuery;
import cn.boho.framework.po.CQueryFactoryTool;
import cn.boho.framework.po.ComboPager;
import cn.boho.framework.po.DAO;
import cn.boho.framework.po.DynaBeanMap;
import cn.boho.framework.po.Pager;

import com.boho.ecp.constant.Product;
import com.boho.ecp.util.MatchUtils;
            
        /**
 * 【商品信息:T_PRODUCT】的Dao操作类
 * 
 */
public class TProductDao extends DAO {
	/**
	* 根据条件查询【商品信息:T_PRODUCT|】信息
	* @param con
	* @param orderBy 排序规则
	* @param productKeyword 关键字
	* @param categoryName 类别名称
	* @return
	* @throws Exception
	* @date2012-07-30
	* @author 高宇舟
	* 
	*/
	public static ComboPager queryTProductByKeywordByPager(Connection con,Pager pager,Integer orderBy,String productKeyword,String categoryName)throws Exception{
		CQExp cqExp = CQExp.instance().select("select T_PRODUCT.CATEGORY_NAME,T_PRODUCT.PRODUCT_BARCODE,T_PRODUCT.PRODUCT_ID,T_PRODUCT.MEMBER_PRICE,T_PRODUCT.REMARKS,T_PRODUCT.PRODUCT_STATUS,T_PRODUCT.PRODUCT_NAME,T_PRODUCT.PRODUCT_ITEM_NO,T_PRODUCT.PHOTO_URL,T_PRODUCT.PRODUCT_NO,T_PRODUCT.BRAND_ID,T_PRODUCT.MARKET_PRICE,T_PRODUCT.BRAND_NAME,T_PRODUCT.RETUNS_TIMES,T_PRODUCT.IS_VALID,T_PRODUCT.BUY_TIMES,T_PRODUCT.CLICK_TIMES,T_PRODUCT.CATEGORY_ID,T_PRODUCT.PRODUCT_TAG,T_PRODUCT.PRODUCT_KEYWORD,T_PRODUCT.FAVORITES_TIMES,T_PRODUCT.MIND_TIME from T_PRODUCT ")
			.where()
			.filed("T_PRODUCT.IS_VALID",CQExp.EQ,1);
			if (!MatchUtils.isEmpty(productKeyword)) {
				String[] keywords = productKeyword.split("/");
				if (keywords.length > 1) {
					cqExp.str(" and (");
					for(int i=0,j=keywords.length; i<j; i++) {
						if (i > 1) {
							cqExp.str(" OR ");
						}
						cqExp.str("T_PRODUCT.PRODUCT_KEYWORD like '%"+keywords[i]+"%'");
						if (i == 0) {
							cqExp.str(" OR ");
						}
					}
					cqExp.str(")");
				} else {
					cqExp.filed(productKeyword == null || productKeyword.equals("") ? null:"T_PRODUCT.PRODUCT_KEYWORD",CQExp.LIKE,"%"+productKeyword+"%")
					;
				}
			}
			if (!MatchUtils.isEmpty(categoryName)) {
				String[] categorys = categoryName.split("/");
				if (categorys.length > 1) {
					cqExp.str(" and (");
					for(int i=0,j=categorys.length; i<j; i++) {
						if (i > 1) {
							cqExp.str(" AND ");
						}
						cqExp.str("T_PRODUCT.PRODUCT_KEYWORD like '%"+categorys[i]+"%'");
						if (i == 0) {
							cqExp.str(" AND ");
						}
					}
					cqExp.str(")");
				} else {
					cqExp.filed(categoryName == null || categoryName.equals("") ? null:"T_PRODUCT.PRODUCT_KEYWORD",CQExp.LIKE,"%"+categoryName+"%")
					;
				}
			}
		switch (orderBy) {
			// 按销量从高到低
			case 1:
				cqExp.orderByDesc("T_PRODUCT.BUY_TIMES").orderByDesc("T_PRODUCT.CREATE_DATE");
				break;
			// 按销量从低到高
			case 2:
				cqExp.orderByAsc("T_PRODUCT.BUY_TIMES").orderByDesc("T_PRODUCT.CREATE_DATE");
				break;
			// 按价格从高到低
			case 3:
				cqExp.orderByDesc("T_PRODUCT.MEMBER_PRICE").orderByDesc("T_PRODUCT.CREATE_DATE");
				break;
			// 按价格从低到高
			case 4:
				cqExp.orderByAsc("T_PRODUCT.MEMBER_PRICE").orderByDesc("T_PRODUCT.CREATE_DATE");
				break;
			default:
				cqExp.orderByDesc("T_PRODUCT.CREATE_DATE");
				break;
		}
		System.out.println(cqExp.getSql());
		return cqExp.getDynaBeanMapComboPager("T_PRODUCT",pager,con);
	}

	/**
	* 根据条件查询【商品信息:T_PRODUCT|】信息
	* @param con
	* @param productKeyword 关键字
	* @param categoryId 类目编号
	* @return
	* @throws Exception
	* @date2012-07-30
	* @author 高宇舟
	* 
	*/
	public static List queryTProductByKeyword(Connection con,java.lang.String productKeyword,String categoryId)throws Exception{
		CQExp cqExp = CQExp.instance().select("select T_PRODUCT.CATEGORY_NAME,T_PRODUCT.PRODUCT_BARCODE,T_PRODUCT.PRODUCT_ID,T_PRODUCT.MEMBER_PRICE,T_PRODUCT.REMARKS,T_PRODUCT.PRODUCT_STATUS,T_PRODUCT.PRODUCT_NAME,T_PRODUCT.PRODUCT_ITEM_NO,T_PRODUCT.PHOTO_URL,T_PRODUCT.PRODUCT_NO,T_PRODUCT.BRAND_ID,T_PRODUCT.MARKET_PRICE,T_PRODUCT.BRAND_NAME,T_PRODUCT.RETUNS_TIMES,T_PRODUCT.IS_VALID,T_PRODUCT.BUY_TIMES,T_PRODUCT.CLICK_TIMES,T_PRODUCT.CATEGORY_ID,T_PRODUCT.PRODUCT_TAG,T_PRODUCT.PRODUCT_KEYWORD,T_PRODUCT.FAVORITES_TIMES,T_PRODUCT.MIND_TIME from T_PRODUCT ")
		.where()
		.filed("T_PRODUCT.IS_VALID",CQExp.EQ,1)
		;
		if (!MatchUtils.isEmpty(productKeyword)) {
			String[] keywords = productKeyword.split("/");
			if (keywords.length > 1) {
				cqExp.str(" and (");
				for(int i=0,j=keywords.length; i<j; i++) {
					if (i > 1) {
						cqExp.str(" OR ");
					}
					cqExp.str("T_PRODUCT.PRODUCT_KEYWORD like '%"+keywords[i]+"%'");
					if (i == 0) {
						cqExp.str(" OR ");
					}
				}
				cqExp.str(")");
			} else {
				cqExp.filed(productKeyword == null || productKeyword.equals("") ? null:"T_PRODUCT.PRODUCT_KEYWORD",CQExp.LIKE,"%"+productKeyword+"%")
				;
			}
		}
		if (!MatchUtils.isEmpty(categoryId)) {
			cqExp.str(" and T_PRODUCT.CATEGORY_ID in (").str(categoryId).str(")");
		}
		System.out.println(cqExp.getSql());
		return cqExp.getDynaBeanMapList("T_PRODUCT",con);
	}
	
	/**
	* 根据条件查询【商品信息:T_PRODUCT|】信息
	* @param con
	* @param productId 商品标识
	* @return
	* @throws Exception
	* @date2012-08-01
	* @author 高宇舟
	* 
	*/
	public static DynaBeanMap queryTProductByCondition(Connection con,java.lang.Integer productId)throws Exception{
		CQExp cqExp = CQExp.instance().select("select * from T_PRODUCT ")
			.where()
			.filed("T_PRODUCT.PRODUCT_ID",CQExp.EQ,productId)
			.filed("T_PRODUCT.IS_VALID",CQExp.EQ,1)
			;
		System.out.println(cqExp.getSql());
		return cqExp.getDynaBeanMap("T_PRODUCT",con);
	}

	/**
	 * 根据ID查询商品信息
	 * @param con
	 * @param ids
	 * @return
	 * @throws Exception
	 * @date 2012-09-25
	 * @author 陈伟 
	 */
	public static List queryProductByIds(Connection con, String ids) throws Exception{
		List list = new LinkedList();
		StringBuffer sql = new StringBuffer();
		sql.append("select * from T_PRODUCT where PRODUCT_ID in ("+ids+") and IS_VALID = ?");
		CQuery query = CQueryFactoryTool.createFactory().createCQuery();
		query.setInt(1, Product.VAILD); 
		query.setCommand(sql.toString());
		list = query.getDynaBeanMapList("T_PRODUCT", con);
		return list;
	}

	/**
	 * 根据商品编号更新点击次数
	 * @param con
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static int updateClickByProductId(Connection con, Integer productId) throws Exception {
		String sql = "update T_PRODUCT set CLICK_TIMES = (CLICK_TIMES + 1) where PRODUCT_ID = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setInt(1, productId);
		return ps.executeUpdate();
	}
	
	/**
	 * 根据商品编号更新购买次数
	 * @param con
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static int updateBuyByProductId(Connection con, Integer productId) throws Exception {
		String sql = "update T_PRODUCT set BUY_TIMES = (BUY_TIMES + 1) where PRODUCT_ID = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setInt(1, productId);
		return ps.executeUpdate();
	}
	
	/**
	 * 根据商品编号列表更新购买次数
	 * @param con
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	public static int batchUpdateBuyByProductIds(Connection con, String ids) throws Exception{
		String sql = "update T_PRODUCT set BUY_TIMES = (BUY_TIMES + 1) where PRODUCT_ID in(" + ids + ")";
		PreparedStatement ps = con.prepareStatement(sql);
		return ps.executeUpdate();
	}
	
	/**
	 * 根据商品编号更新意向次数
	 * @param con
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static int updateMindByProductId(Connection con, Integer productId) throws Exception {
		String sql = "update T_PRODUCT set MIND_TIME = (MIND_TIME + 1) where PRODUCT_ID = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setInt(1, productId);
		return ps.executeUpdate();
	}
	
	/**
	 * 根据商品编号更新收藏次数
	 * @param con
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static int updateFavByProductId(Connection con, Integer productId) throws Exception {
		String sql = "update T_PRODUCT set FAVORITES_TIMES = (FAVORITES_TIMES + 1) where PRODUCT_ID = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setInt(1, productId);
		return ps.executeUpdate();
	}
	
	/**
	 * 根据商品编号更新反馈次数
	 * @param con
	 * @param productId
	 * @return
	 * @throws Exception
	 */
	public static int updateReturnByProductId(Connection con, Integer productId) throws Exception {
		String sql = "update T_PRODUCT set RETUNS_TIMES = (RETUNS_TIMES + 1) where PRODUCT_ID = ?";
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setInt(1, productId);
		return ps.executeUpdate();
	}
	
	public static int batchUpdateReturnByProductIds(Connection con, String ids) throws Exception {
		String sql = "update T_PRODUCT set RETUNS_TIMES = (RETUNS_TIMES + 1) where PRODUCT_ID in ("+ ids +")";
		PreparedStatement ps = con.prepareStatement(sql);
		return ps.executeUpdate();
	}
}
